package com.tensquare.base.controller;

import com.tensquare.base.pojo.Label;
import com.tensquare.base.service.LabelService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.hibernate.validator.constraints.EAN;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

@RestController
@CrossOrigin
@RefreshScope
@RequestMapping("/label")
public class LabelController {

    @Autowired
    private LabelService labelService;

    @Autowired
    private HttpServletRequest request;

    @Value("${sms.ip}")
    private String ip;



    @RequestMapping(method = RequestMethod.GET)
    public Result findAll(){

        System.out.println(ip);
        return new Result(true, StatusCode.OK,"查询成功",labelService.findAll());
    }

    @RequestMapping(value = "/{labelId}",method = RequestMethod.GET)
    public  Result findByid(@PathVariable("labelId") String labelId){
        System.out.println("111111111111111111");
        return new Result(true,StatusCode.OK,"通过id查询成功",labelService.findByid(labelId));
    }

    @RequestMapping(method = RequestMethod.POST)
    public Result insret(@RequestBody Label label){
        labelService.insert(label);
        return new Result(true,StatusCode.OK,"添加成功");
    }

      @RequestMapping(value = "/{labelId}",method = RequestMethod.PUT)
      public Result updateByid(@RequestBody Label label,@PathVariable("labelId") String labelId){
       label.setId(labelId);
        labelService.update(label);
        return new Result(true,StatusCode.OK,"修改成功");
     }

     @RequestMapping(value = "/{labelId}",method = RequestMethod.DELETE)
    public Result deleteBYid(@PathVariable("labelId") String labelid){
        labelService.deleteByid(labelid);
        return new Result(true,StatusCode.OK,"通过id删除成功");
     }

     @RequestMapping(value = "/search",method = RequestMethod.POST)
    public Result findSearch(@RequestBody Label label){
        List<Label> list = labelService.findSearch(label);




         return new Result(true,StatusCode.OK,"查询条件",list);
     }

      @RequestMapping(value = "/search/{page}/{size}",method = RequestMethod.POST)
    public Result pageQuery(@PathVariable int page,@PathVariable int size,@RequestBody Label label){
       Page<Label> pageData= labelService.pageQuery(label,page,size);

        return new Result(true,StatusCode.OK,"查询分页",new PageResult<Label>(pageData.getTotalElements(),pageData.getContent()));
     }






}
